<?php

//namespace model;

require_once("model/StickSelection.php");
require_once("model/AIPlayer.php");
require_once("model/PersistantSticks.php");

class LastStickGame {

const StartingNumberOfSticks = 30;
    public function __construct() {
        $this->ai = new AIPlayer();
        $this->sticks = new PersistantSticks(self::StartingNumberOfSticks); //FEL 7  "har fixatfel"
    }

    public function playerSelectsSticks(StickSelection $playerSelection, StickGameObserver $observer) {
        $this->sticks->removeSticks($playerSelection);

        if ($this->isGameOver()) {
            $this->playerWins(); //FEL11 anrop till en function i view, men nu har jag flyttat den in till model/LastStickGame se rad64.
        } else {
            $this->AIPlayerTurn($observer);
        }
    }

    private function AIPlayerTurn(StickGameObserver $observer) {
        $sticksLeft = $this->getNumberOfSticks();
        $selection = $this->ai->getSelection($sticksLeft);

        $this->sticks->removeSticks($selection);
        $observer->aiRemoved($selection);  //aiRem

        if ($this->isGameOver()) {
            $this->playerLoose();
        }
    }

    /**
    * @return boolean
    */
    public function isGameOver() {
        return $this->sticks->getNumberOfSticks() < 2;
    }

    /**
    * @return int
    */
    public function getNumberOfSticks() {
        return $this->sticks->getNumberOfSticks();
    }

    public function newGame() {
        $this->sticks->newGame(self::StartingNumberOfSticks); //FEL 7 & FEL 9
    }


      /**
    * @var boolean
    * FEL 10 dessa funktioner flyttades från GameView till modellen.
    */
    private $playerWon = false;

    public function playerWins() {  //FEL 11 fixat denna function låg i view innan
        $this->playerWon = true;
    }
    public function playerLoose() {
        $this->playerWon = false;
    }

     public function playerHasWon() {
        return $this->playerWon;
    }





}